
package com.oahcfly.chgame.core.manager;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.utils.Disposable;
import com.badlogic.gdx.utils.ObjectMap;

/** 支持格式： WAV, MP3 and OGG. 大小不超过1M
 * 
 * @author haocao */
public class SoundManager implements Disposable {
	ObjectMap<String, Sound> sounds = new ObjectMap<String, Sound>();

	public void loadSound (String res) {
		Sound soundFromAssets = Gdx.audio.newSound(Gdx.files.internal(res));
		if (null != soundFromAssets) {
			this.sounds.put(res, soundFromAssets);
		} else {
			Gdx.app.error(getClass().getSimpleName(), "can not find " + res);
		}
	}

	float soundVolume = 1f;

	public float getSoundVolume () {
		return soundVolume;
	}

	public void setVolume (float volume) {
		this.soundVolume = volume;
	}

	public void stopSound (final String res) {
		this.sounds.get(res).stop();
	}

	public long playSound (final String res) {
		return this.playSound(res, soundVolume);
	}

	public long playSound (final String res, final float soundVolume) {
		final Sound soundFromPool = this.sounds.get(res);
		if (null == soundFromPool) {
			Sound soundFromAssets = Gdx.audio.newSound(Gdx.files.internal(res));
			if (null != soundFromAssets) {
				this.sounds.put(res, soundFromAssets);
				return this.playSound(soundFromAssets, soundVolume);
			} else {
				Gdx.app.error(getClass().getSimpleName(), "can not find " + res);
			}
		} else {
			return this.playSound(soundFromPool, soundVolume);
		}
		return -1;
	}

	private long playSound (final Sound sound, final float soundVolume) {
		return sound.play(soundVolume);
	}

	public void dispose () {
		for (Sound sound : sounds.values()) {
			if (null != sound) sound.dispose();
		}
		this.sounds.clear();
	}
}
